﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Presentation;
using Infrastructure;
using Domain;

namespace WebClient
{
    public partial class ConverterForm : System.Web.UI.Page, IView, ILog
    {
        private static LogText logText;
        private static LogHtml logHtml;

        public event Action OnConvertClick;
        public event Action OnSaveQuotationsClick;
        public event Action OnAddCurrencyClick;
        public event Action<ILog, bool> OnLogCheckedChanged;

        protected ConverterForm()
        {
            var root = Server.MapPath("~");
            Directory.SetCurrentDirectory(root);
            if (logText == null) logText = new LogText();
            if (logHtml == null) logHtml = new LogHtml();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Presenter"] != null)
            {
                ((Presenter) Session["Presenter"]).Rebind(this);
                OnLogCheckedChanged(this, true);
            }
            else
            {
                Session.Add("Presenter", new Presenter(this));
                OnLogCheckedChanged(this, true);
                OnLogCheckedChanged(logText, true);
                OnLogCheckedChanged(logHtml, true);
            }
        }

        public string FromName
        {
            get { return fromList.Text; }
        }

        public string FromValue
        {
            get { return fromValue.Text; }
        }

        public string ToName
        {
            get { return toList.Text; }
        }

        public string ToValue
        {
            set { toValue.Text = value; }
        }

        public string[] Currencies
        {
            get
            {
                var arr = new object[fromList.Items.Count];
                fromList.Items.CopyTo(arr, 0);
                return (string[]) arr;
            }

            set
            {
                toList.Items.Clear();
                fromList.Items.Clear();

                var li = Array.ConvertAll<object, ListItem>(value, new Converter<object, ListItem>(ListConverter));
                toList.Items.AddRange(li);
                fromList.Items.AddRange(li);
            }
        }

        protected static ListItem ListConverter(object o)
        {
            return new ListItem(o.ToString(), o.ToString());
        }

        public Dictionary<string, string> Quotations
        {
            get
            {
                var res = new Dictionary<string, string>();
                for (var i = 0; i < quotationsList.Rows.Count; i++)
                {
                    try
                    {
                        var quotation = (TextBox)quotationsList.FindControl("TextBox_" + i);
                        res.Add(quotationsList.Rows[i].Cells[0].Text, quotation.Text);
                    }
                    catch
                    {
                        res.Add(quotationsList.Rows[i].Cells[0].Text, "not_a_number");
                    }
                }
                return res;
            }
            set
            {
                quotationsList.Rows.Clear();

                var i = 0;
                foreach (var pair in value)
                {
                    var tr = new TableRow();
                    var tc1 = new TableCell();
                    var tc2 = new TableCell();
                    var tb = new TextBox {Text = pair.Value};
                    tc1.Text = pair.Key;

                    tb.ID = "TextBox_" + i;
                    tb.EnableViewState = true;
                    tc2.Controls.Add(tb);

                    tr.Cells.Add(tc1);
                    tr.Cells.Add(tc2);
                    quotationsList.Rows.Add(tr);
                    i++;
                }
            }
        }

        public string NewName
        {
            get { return newName.Text; }
        }

        public string NewValue
        {
            get { return newValue.Text; }
        }

        public string ErrorMessage
        {
            set { errorMessage.Text = value; }
        }

        protected void convertButton_Click(object sender, EventArgs e)
        {
            OnConvertClick();
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            OnSaveQuotationsClick();
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            OnAddCurrencyClick();
        }

        public void ClearError()
        {
            errorMessage.Text = "";
        }

        public string Log
        {
            get
            {
                return logBox.Text;
            }
            set
            {
                logBox.Text += value + Environment.NewLine;
            }
        }

        protected void textLog_CheckedChanged(object sender, EventArgs e)
        {
            OnLogCheckedChanged(logText, textLog.Checked);
        }

        protected void htmlLog_CheckedChanged(object sender, EventArgs e)
        {
            OnLogCheckedChanged(logHtml, htmlLog.Checked);
        }      
    }
}